<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        *{
            font-size: 50px;
        }

    </style>
</head>
<body>
<div style="display: flex;justify-content: space-around">
<fieldset style="display :inline-block;">
    <legend>注册</legend>
<table >
    <tr >

        <td>
            名字
        </td>
        <td>
            <input  id="regist_name" >
        </td>
    </tr>
    <tr>
        <td>密码</td>
        <td><input  id="regist_pwd" ></td>
    </tr>
    <tr>
        <td colspan="2">
            <button type="button" style="width: 100%" id="regist_btn">注册</button>
        </td>
    </tr>
</table>
</fieldset>
<fieldset style="display :inline-block;">
    <legend>登录</legend>
    <table>
        <tr>
            <td>
                名字
            </td>
            <td>
                <input  id="login_name" >
            </td>
        </tr>
        <tr>
            <td>密码</td>
            <td><input  id="login_pwd"></td>
        </tr>
        <tr>
            <td colspan="2">
                <button type="button" style="width: 100%" id="login_btn">登录</button>
            </td>
        </tr>
    </table>
</fieldset>
</div>
<div>
    <button onclick="getUserInfo(this)" >获取用户信息</button>
    <div></div>
</div>
<div id="cookie"></div>
</body>
<script src="./jquery-2.2.3.min.js"></script>
<script>
    $(function () {
        $("#regist_btn").on("click",function () {

            regist()
        })
        $("#login_btn").on("click",function () {
            login()
        })


    })
    function regist() {
        future.ajax({
            url:'/users/regist',
            type:'post',
            data:{
                name:$("#regist_name").val(),
                pwd:$("#regist_pwd").val()
            },
            success:function (result) {
                alert(result)
            }

        })
    }
    function login() {
        future.ajax({
            url:'/users/login',
            type:'get',
            data:{
                name:$("#login_name").val(),
                pwd:$("#login_pwd").val()
            },
            success:function (result) {
                alert(result)
            }

        })
    }
    function getUserInfo(obj){
        future.ajax({
            url:'/users/getUserInfo',
            type:'get',

            success:function (result) {
                $(obj).siblings('div').text(JSON.stringify(result))
                $("#cookie").text("key:" +result.cookieKey+"    value:"+getCookie(result.cookieKey))
            }
        })
    }

    let  future={}
    future.baseurl="http://localhost:3000"
    future.ajax=function (options) {
        $.ajax({
            beforeSend:function(request){
                request.setRequestHeader("token","xiejin")
            },
            url:future.baseurl+options.url,
            type:options.type ||"post",
            async:options.async||false,
            data:options.data,
            success:function (result) {
                if (options.success) {
                    options.success(result)
                }
            },
            error:function (error) {
                if (options.error){

                    options.error(error)
                }else {
                    alert(error.responseText)
                }

            }

        })
    }



    /**
     * 设置cookie
     */
    function setCookie(name, value, seconds) {
        seconds = seconds || 0;
        var expires = "";
        // cookies随浏览器关闭而失效的方法:
        //     如果不设置Expires的属性那么Cookie的存活时间就是在关闭浏览器的时候。
        if (seconds != 0 ) {      //设置cookie生存时间
            var date = new Date();
            date.setTime(date.getTime()+(seconds*1000));
            expires = "; expires="+date.toGMTString();
        }
        document.cookie = name+"="+escape(value)+expires+"; path=/";   //转码并赋值
    }

    /**
     * 读取cookie
     */
    function getCookie(name) {
        var nameEQ = name + '='
        var ca = document.cookie.split(';') // 把cookie分割成组
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i] // 取得字符串
            while (c.charAt(0) == ' ') { // 判断一下字符串有没有前导空格
                c = c.substring(1, c.length) // 有的话，从第二位开始取
            }
            if (c.indexOf(nameEQ) == 0) { // 如果含有我们要的name
                return unescape(c.substring(nameEQ.length, c.length)) // 解码并截取我们要值
            }
        }
        return false
    }

    /**
     * 检查cookie
     * @param c_name
     * @return {boolean}
     */
    function checkCookie(c_name) {
        username = getCookie(c_name);
        console.log(username);
        if (username != null && username != "") {
            return true;
        }
        else {
            return false;
        }
    }

    /**
     * 清除cookie
     * @param name
     */
    function clearCookie(name) {
        setCookie(name, "", -1);
    }


</script>

</html>
